#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""
@Introduce :
@File      : writetoexcel.py
@Time      : 2022/6/16 20:21
@Author    : toby
"""

import logging
logger = logging.getLogger(__name__)

import pandas as pd
import os

class OperaExcel():
    def __init__(self,filename):
        self.filename= filename
        if not os.path.exists(self.filename):
            os.system(r"touch {}".format(self.filename))  # 调用系统命令行来创建文件
            pf = pd.DataFrame()
            pf.to_excel(self.filename, encoding='utf-8', index=False)

    # 插入数据
    def inset_row(self,data):
        pf = pd.DataFrame()
        # 设置列 值  表头
        pf['Type'] = list(data.keys())
        pf['Path'] = list(data.values())
        # index 为FALSE 表示没有第一列数字为索引
        pf.to_excel(self.filename, encoding='utf-8', index=False)

    # 追加数据
    def concat(self,data):
        # 读取现在的数据
        rdf = pd.read_excel(self.filename)
        # 新增数据
        pf = pd.DataFrame()
        # 设置列 值  表头
        pf['Type'] = list(data.keys())
        pf['Path'] = list(data.values())

        # 合并后数据
        rdfnew = pd.concat([rdf,pf],ignore_index=True)
        print(rdfnew)
        # 写到表中
        rdfnew.to_excel(self.filename, encoding='utf-8', index=False)

if __name__ == '__main__':
    filename =r"/root/flask-celery-hjw/flask-celery/pom_framwork/element-data/uielement.xlsx"
    data ={'By.NAME':'//input[@name="password" and @placeholder="输入密码"]',
           "By.ID":"//div[@id='normalLoginTab']/div[0]/div[0]/iframe",
           "By.class":"./xxxx/xxx[0]"}
    op = OperaExcel(filename)
    # 插入
    op.inset_row(data=data)
    # 追加数据
    op.concat(data=data)


